package com.exartisan.yn_event;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.Signature;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.util.Base64;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.EditText;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.ActionBar.TabListener;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.exartisan.adapter.HomePagerAdapter;
import com.exartisan.network.RestConnector;
import com.exartisan.ynevent.BaseActivity;

public class HomeActivity extends BaseActivity implements TabListener {
	private static final String TAG = "DialogActivity";
	private static final int DLG_EXAMPLE1 = 0;
	private static final int TEXT_ID = 0;
	private ViewPager pager;
	private HomePagerAdapter homeAdapter;

	@Override
	protected void setUpActionBar() {
		super.setUpActionBar();
		getSupportActionBar().setDisplayShowTitleEnabled(false);
		getSupportActionBar().setDisplayShowHomeEnabled(false);
		getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
	}

	@Override
	protected void initUI() {
		setContentView(R.layout.activity_home);
		pager = (ViewPager) findViewById(R.id.pager);
	}

	@Override
	protected void initProcess() {
		try {
			PackageInfo info;
			info = getPackageManager().getPackageInfo("com.exartisan.yn_event",
					PackageManager.GET_SIGNATURES);
			for (Signature signature : info.signatures) {
				MessageDigest md = MessageDigest.getInstance("SHA");
				md.update(signature.toByteArray());
				Log.d("KeyHash:",
						Base64.encodeToString(md.digest(), Base64.DEFAULT));
			}
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		homeAdapter = new HomePagerAdapter(getSupportFragmentManager());
		pager.setAdapter(homeAdapter);
		pager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
			@Override
			public void onPageSelected(int pos) {
				super.onPageSelected(pos);
				getSupportActionBar().setSelectedNavigationItem(pos);
			}
		});
		for (int i = 0; i < homeAdapter.getCount(); i++) {
			getSupportActionBar().addTab(
					getSupportActionBar().newTab()
							.setText(homeAdapter.getPageTitle(i))
							.setTabListener(this));
		}
	}

	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		pager.setCurrentItem(tab.getPosition());
	}

	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {

	}

	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) {

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_MENU) {
			openOptionsMenu();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		showDialog(DLG_EXAMPLE1);
		return true;
	}

	@Override
	protected Dialog onCreateDialog(int id) {

		switch (id) {
		case DLG_EXAMPLE1:
			return createExampleDialog();
		default:
			return null;
		}
	}

	private Dialog createExampleDialog() {

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("Set IP");
		builder.setMessage("Nhập địa chỉ IP:");

		// Use an EditText view to get user input.
		final EditText input = new EditText(this);
		input.setText(RestConnector.IP);
		input.setId(TEXT_ID);
		builder.setView(input);

		builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int whichButton) {
				String value = input.getText().toString().trim();
				RestConnector.IP = value;
				RestConnector.resetURL();
				return;
			}
		});

		builder.setNegativeButton("Cancel",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						return;
					}
				});

		return builder.create();
	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
		final com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
		inflater.inflate(R.menu.home, menu);
		return true;
	}

}
